﻿using ShareCode.Entity;
using ShareCode.KeyValue;
using ShareCode.Util.Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShareCode.Util.Helper
{
    public static class MergeVersionDataHelper
    {
        public static List<TT> MergeVersionData<TT>(List<TT> data) where TT : BaseBaseEntity, IRepairEntity, new()
        {
            List<TT> reData = [];

            if (data == default || data.Count < 1) return reData;

            //取出全部版本Id
            List<long> versionIds = [];
            versionIds = data.MyDistinct((a, b) => a.VersionId == b.VersionId).Select(t => t!.VersionId).ToList();
            versionIds.Sort();

            reData = data.Where(t => t.VersionId == versionIds[0]).ToList();
            if (versionIds.Count < 2)
            {
                return reData;
            }

            for (int i = 1; i < versionIds.Count; i++)
            {
                var tempData = data.Where(t => t.VersionId == versionIds[i]).OrderBy(t => t.Id).ToList();
                foreach (var a in tempData)
                {
                    //合并删除
                    if (a.RepairMethod == (int)EnumRepairMethod.Delete)
                    {
                        var obj = reData.FirstOrDefault(t => t.Id == a.RepairTargetId);
                        if (obj != default)
                        {
                            reData.Remove(obj);
                        }
                    }

                    //合并更新
                    if (a.RepairMethod == (int)EnumRepairMethod.Update)
                    {
                        var obj = reData.FirstOrDefault(t => t.Id == a.RepairTargetId);

                        if (obj != default)
                        {
                            obj.Id = a.Id;
                            reData.Remove(a);
                            reData.Add(obj);
                        }
                    }

                    //合并添加
                    if (a.RepairMethod == (int)EnumRepairMethod.AddBefore)
                    {
                        reData.Add(a);
                    }
                    if (a.RepairMethod == (int)EnumRepairMethod.AddAfter)
                    {
                        reData.Add(a);
                    }
                }
            }

            return reData;
        }

        public static List<TT> MergeVersionData2<TT>(List<TT> data) where TT : BaseBaseEntity, IRepair2Entity, new()
        {
            List<TT> reData = [];

            if (data == default || data.Count < 1) return reData;

            //取出全部版本Id
            List<long> versionIds = [];
            versionIds = data.MyDistinct((a, b) => a.VersionId == b.VersionId).Select(t => t!.VersionId).ToList();
            if (versionIds.Count < 2)
            {
                return reData;
            }

            versionIds.Sort();
            reData = data.Where(t => t.VersionId == versionIds[0]).ToList();


            for (int i = 1; i < versionIds.Count; i++)
            {
                var tempData = data.Where(t => t.VersionId == versionIds[i] && t.CorpusSenseId == 0).OrderBy(t => t.Id).ToList();
                foreach (var a in tempData)
                {
                    //合并删除
                    if (a.RepairMethod == (int)EnumRepairMethod.Delete)
                    {
                        var obj = reData.FirstOrDefault(t => t.Id == a.RepairTargetId);
                        if (obj != default)
                        {
                            reData.Remove(obj);
                        }
                    }

                    //合并更新
                    if (a.RepairMethod == (int)EnumRepairMethod.Update)
                    {
                        var obj = reData.FirstOrDefault(t => t.Id == a.RepairTargetId);

                        if (obj != default)
                        {
                            obj.Id = a.Id;
                            reData.Remove(a);
                            reData.Add(obj);
                        }
                    }

                    //合并添加
                    if (a.RepairMethod == (int)EnumRepairMethod.AddBefore)
                    {
                        reData.Add(a);
                    }
                    if (a.RepairMethod == (int)EnumRepairMethod.AddAfter)
                    {
                        reData.Add(a);
                    }
                }
            }

            List<long> corpusSenseIds = [];
            corpusSenseIds = data.MyDistinct((a, b) => a.CorpusSenseId == b.CorpusSenseId).Select(t => t!.CorpusSenseId).ToList();
            corpusSenseIds.Sort();
            if (corpusSenseIds.Count < 2)
            {
                return reData;
            }

            for (int i = 1; i < corpusSenseIds.Count; i++)
            {
                var tempData = data.Where(t => t.CorpusSenseId == corpusSenseIds[i] && t.CorpusSenseId == 0).OrderBy(t => t.Id).ToList();
                foreach (var a in tempData)
                {
                    //合并删除
                    if (a.RepairMethod == (int)EnumRepairMethod.Delete)
                    {
                        var obj = reData.FirstOrDefault(t => t.Id == a.RepairTargetId);
                        if (obj != default)
                        {
                            reData.Remove(obj);
                        }
                    }

                    //合并更新
                    if (a.RepairMethod == (int)EnumRepairMethod.Update)
                    {
                        var obj = reData.FirstOrDefault(t => t.Id == a.RepairTargetId);

                        if (obj != default)
                        {
                            obj.Id = a.Id;
                            reData.Remove(a);
                            reData.Add(obj);
                        }
                    }

                    //合并添加
                    if (a.RepairMethod == (int)EnumRepairMethod.AddBefore)
                    {
                        reData.Add(a);
                    }
                    if (a.RepairMethod == (int)EnumRepairMethod.AddAfter)
                    {
                        reData.Add(a);
                    }
                }
            }



            return reData;
        }
    }
}
